<template>
  <div>
    <div class="topbar">
      <div class="nav">
        <ul>
          <li v-if="!this.$store.getters.getUser">
            <el-button type="text" @click="login">登录</el-button>
            <span class="sep">|</span>
            <el-button type="text" @click="Register">注册</el-button>
          </li>
          <li v-else>
            欢迎
            <el-popover placement="top" width="180" v-model="visible">
              <p>确定退出登录吗？</p>
              <div style="text-align: right; margin: 10px 0 0">
                <el-button size="mini" type="text" @click="visible = false">取消</el-button>
                <el-button type="primary" size="mini" @click="logout">确定</el-button>
              </div>
              <el-button type="text" slot="reference">{{this.$store.getters.getUser.userName}}</el-button>
            </el-popover>
          </li>
          <li>
            <router-link to="/order">我的订单</router-link>
          </li>
          <li>
            <router-link to="/collect">我的收藏</router-link>
          </li>
          <li :class="getNum > 0 ? 'shopCart-full' : 'shopCart'">
            <router-link to="/shoppingCart">
              <i class="el-icon-shopping-cart-full"></i> 购物车
              <span class="num">({{getNum}})</span>
            </router-link>
          </li>
        </ul>
      </div>
    </div>

    <!-- 注册模块 -->
    <!-- 双向传值，课接受子组件的传来的register值（子组件传值方式this.$emit("update:register",val)） -->
    <MyRegister :register.sync="register"></MyRegister>
  </div>
</template>
<script>
import MyRegister from "components/common/myregister/MyRegister.vue";
import { mapGetters, mapActions } from "vuex";

export default {
  data() {
    return {
      register: false, // 注册组件的显示
      visible: false //退出登录的显示
    };
  },
  created() {
    // 获取浏览器localStorage，判断用户是否已经登录
    if (localStorage.getItem("user")) {
      // 如果已经登陆，设置vuex的登录状态
      this.setUser(JSON.parse(localStorage.getItem("user")));
    }
  },
  computed: {
    ...mapGetters(["getNum", "getUser"])
  },
  watch: {
    getUser(val) {
      if (val === "") {
        // 用户没有登陆
        this.setShoppingCart([]);
      } else {
        // 用户已经登录,获取该用户的购物车信息
        this.$axios
          .post("/user/shoppingCart/getShoppingCart", {
            user_id: val.user_id
          })
          .then(res => {
            if (res.code === "001") {
              // 001 为成功, 更新vuex购物车状态
              console.log(res, "resssssss");
              this.setShoppingCart(res.shoppingCartData);
            } else {
              // 提示失败信息
              this.notifyError(res.msg);
            }
          })
          .catch(err => {
            return Promise.reject(err);
          });
      }
    }
  },
  methods: {
    ...mapActions(["setShowLogin", "setUser", "setShoppingCart"]),
    login() {
      console.log("登陆");
      // 调用Vuex方法
      this.setShowLogin(true);
    },
    Register() {
      console.log("注册点击");
      this.register = true;
    },
    logout() {
      this.visible = false;
      // 清空本地登录信息
      localStorage.setItem("user", "");
      // 清空vuex登录信息
      this.setUser("");
      this.notifySucceed("成功退出登录");
    }
  },
  components: {
    MyRegister
  }
};
</script>
<style>
/* 顶部导航栏CSS */
.topbar {
  width: 100%;
  height: 40px;
  background-color: #3d3d3d;
  margin-bottom: 20px;
}
.topbar .nav {
  width: 1225px;
  margin: 0 auto;
}
.topbar .nav ul {
  float: right;
}
.topbar .nav li {
  float: left;
  height: 40px;
  color: #b0b0b0;
  font-size: 14px;
  text-align: center;
  line-height: 40px;
  margin-left: 20px;
}
.topbar .nav .sep {
  color: #b0b0b0;
  font-size: 12px;
  margin: 0 5px;
}
.topbar .nav li .el-button {
  color: #b0b0b0;
}
.topbar .nav .el-button:hover {
  color: #fff;
}
.topbar .nav li a {
  color: #b0b0b0;
}
.topbar .nav a:hover {
  color: #fff;
}
.topbar .nav .shopCart {
  width: 120px;
  background: #424242;
}
.topbar .nav .shopCart:hover {
  background: #fff;
}
.topbar .nav .shopCart:hover a {
  color: #ff6700;
}
.topbar .nav .shopCart-full {
  width: 120px;
  background: #ff6700;
}
.topbar .nav .shopCart-full a {
  color: white;
}
/* 顶部导航栏CSS END */
</style>